<template>
  <div class="app-container">

    <div class="tabInfo">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="角色名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入角色名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-show="checkType === 'multiple'"
      ref="dataTable"
      v-loading="loading"
      :data="roleList"
      @selection-change="handleMultipleRoleSelect"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="角色编号" prop="roleId" width="120" />
      <el-table-column
        label="角色名称"
        prop="roleName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="权限字符"
        prop="roleKey"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column label="显示顺序" prop="roleSort" width="100" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-table
      v-show="checkType === 'single'"
      v-loading="loading"
      :data="roleList"
      @current-change="handleSingleRoleSelect"
    >
      <el-table-column width="55" align="center">
        <template #default="scope">
          <el-radio v-model="radioSelected" :label="scope.row.roleId">{{
            ""
          }}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="角色编号" prop="roleId" width="120" />
      <el-table-column
        label="角色名称"
        prop="roleName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="权限字符"
        prop="roleKey"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column label="显示顺序" prop="roleSort" width="100" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page-sizes="[5, 10]"
      layout="prev, pager, next"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    </div>
  </div>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { listRole } from "@/api/system/role";
import { StrUtil } from "@/utils/StrUtil";

const emit = defineEmits(['handleRoleSelect']);
const queryForm = ref(null);
const dataTable = ref(null);
const queryParams = ref({
  pageNum: 1,
  pageSize: 5,
  roleName: undefined,
  roleKey: undefined,
  status: undefined,
});
const roleList = ref([]);
const loading = ref(true);
const total = ref(0);
const showSearch = ref(true);
const radioSelected = ref(0);
const selectRoleList = ref([]);
const checkType = ref('multiple');

const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

const resetQuery = () => {
  queryForm.value.resetFields();
  handleQuery();
};

const handleMultipleRoleSelect = (selection) => {
  console.log(selection,'1')
  const idList = selection.map((item) => item.roleId);
  const nameList = selection.map((item) => item.roleName);
  emit('handleRoleSelect', idList.join(','), nameList.join(','));
};

const handleSingleRoleSelect = (selection) => {
  console.log(selection,'2')
  radioSelected.value = selection.roleId;
  const roleName = selection.roleName;
  emit('handleRoleSelect', radioSelected.value.toString(), roleName);
};

const getList = () => {
  loading.value = true;
  listRole(queryParams.value).then((response) => {
    roleList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
};

watch(() => queryParams.value.selectValues, (newVal) => {
  if (StrUtil.isNotBlank(newVal)) {
    if (typeof newVal === 'number' || typeof newVal === 'string') {
      radioSelected.value = newVal;
    } else if (Array.isArray(newVal)) {
      selectRoleList.value = newVal;
    }
  }
}, { immediate: true });

watch(roleList, (newVal) => {
  if (newVal && selectRoleList.value.length > 0) {
    nextTick(() => {
      const dataTableInstance = dataTable.value;
      selectRoleList.value.split(',').forEach((key) => {
        const row = newVal.find((item) => key == item.roleId);
        if (row) {
          dataTableInstance.toggleRowSelection(row, true);
        }
      });
    });
  }
});

onMounted(() => {
  getList();
});

</script>

<style>
/* 隐藏radio展示的label及本身自带的样式 */
/* .el-radio__label {
  display: none;
} */
</style>